﻿using System;
using System.Collections.Generic;
using GameFramework.Resource;
using ObjectPool;

namespace Resource
{
    /// <summary>
    /// 资源管理器接口。
    /// </summary>
    public interface IResourceManager
    {
        /// <summary>
        /// 获取资源只读区路径。
        /// </summary>
        string ReadOnlyPath { get; }

        /// <summary>
        /// 获取资源读写区路径
        /// </summary>
        string ReadWritePath { get; }


        /// <summary>
        /// 获取资源模式。
        /// </summary>
        ResourceMode ResourceMode { get; }


        /// <summary>
        /// 获取当前变体。
        /// </summary>
        string CurrentVariant { get; }

        /// <summary>
        /// 获取当前资源使用的游戏版本号
        /// </summary>
        string ApplicableGameVersion { get; }

        /// <summary>
        /// 获取内部资源版本号
        /// </summary>
        int InternalResourceVersion { get; }

        /// <summary>
        /// 获取资源数量。
        /// </summary>
        int AssetCount { get; }

        /// <summary>
        /// 获取资源数量。
        /// </summary>
        int ResourceCount { get; }

        /// <summary>
        /// 获取资源组数量。
        /// </summary>
        int ResourceGroupCount { get; }

        /// <summary>
        /// 获取或设置资源更新下载地址
        /// </summary>
        string UpdatePrefixUri { get; set; }

        /// <summary>
        /// 获取或设置每更新多少字节的资源就生成一次版本资源列表
        /// </summary>
        int GenerateReadWriteVersionListLength { get; set; }

        /// <summary>
        /// 获取正在应用的资源包路径
        /// </summary>
        string ApplyingResourcePackPath { get; }

        /// <summary>
        /// 获取等待应用资源数量
        /// </summary>
        int ApplyWaitingCount { get; }

        /// <summary>
        /// 获取或设置资源更新重试次数
        /// </summary>
        int UpdateRetryCount { get; set; }

        /// <summary>
        /// 获取正在更新的资源组
        /// </summary>
        IResourceGroup UpdatingResourceGroup { get; }

        /// <summary>
        /// 获取等待更新资源数量
        /// </summary>
        int UpdateWaitingCount { get; }

        /// <summary>
        /// 获取使用时下载的等待更新资源数量
        /// </summary>
        int UpdateWaitingWhilePlayingCount { get; }

        /// <summary>
        /// 获取候选更新资源数量
        /// </summary>
        int UpdateCandidateCount { get; }

        /// <summary>
        /// 获取加载资源代理总数量
        /// </summary>
        int LoadTotalAgentCount { get; }

        /// <summary>
        /// 获取可用加载资源代理数量
        /// </summary>
        int LoadFreeAgentCount
        {
            get;
        }
        
        /// <summary>
        /// 获取工作中加载资源代理数量
        /// </summary>
        int LoadWorkingAgentCount
        {
            get;
        }

        /// <summary>
        /// 获取等待加载资源数量
        /// </summary>
        int LoadWaitingTaskCount
        {
            get;
        }
        
        /// <summary>
        /// 获取或设置资源对象池自动释放可释放对象的间隔秒数
        /// </summary>
        float AssetAutoReleaseInterval
        {
            get;
            set;
        }
        
        /// <summary>
        /// 获取或设置资源对象池的容量
        /// </summary>
        int AssetCapacity
        {
            get;
            set;
        }
        
        /// <summary>
        /// 获取或设置资源对象池对象过期秒数
        /// </summary>
        float AssetExpireTime
        {
            get;
            set;
        }

        /// <summary>
        /// 获取或设置资源对象池自动释放可释放对象的间隔秒数
        /// </summary>
        float ResourceAutoReleaseInterval
        {
            get;
            set;
        }
        
        /// <summary>
        /// 获取设置资源对象池的容量
        /// </summary>
        int ResourcesCapacity
        {
            get;
            set;
        }
        
        /// <summary>
        /// 获取或设置资源对象池对象过期秒数
        /// </summary>
        float ResourceExpireTime
        {
            get;
            set;
        }


        /// <summary>
        /// 获取或设置资源对象池的优先级
        /// </summary>
        int ResourcePriority
        {
            get;
            set;
        }

        /// <summary>
        /// 设置资源只读区路径
        /// </summary>
        /// <param name="readOnlyPath">资源只读区路径</param>
        void SetReadOnlyPath(string readOnlyPath);

        /// <summary>
        /// 设置资源读写区路径
        /// </summary>
        /// <param name="readWritePath"></param>
        void SetReadWritePath(string readWritePath);

        /// <summary>
        /// 设置资源模式
        /// </summary>
        /// <param name="resourceMode"></param>
        void SetResourceMode(ResourceMode resourceMode);
        
        /// <summary>
        /// 设置当前变体
        /// </summary>
        /// <param name="currentVariant"></param>
        void SetCurrentVariant(string currentVariant);
        
        /// <summary>
        /// 设置对象池管理器
        /// </summary>
        /// <param name="objectPoolManager"></param>
        void SetObjectPoolManager(IObjectPoolManager objectPoolManager);

        /// <summary>
        /// 设置文件系统管理器
        /// </summary>
        /// <param name="fileSystemManager"></param>
        void SetFileSystemManager(IFileSystemManager fileSystemManager);

        /// <summary>
        /// 设置下载管理器
        /// </summary>
        /// <param name="downloadManager"></param>
        void SetDownloadManager(IDownloadManager downloadManager);
        
        /// <summary>
        /// 设置解密资源回调函数, 如果不设置，将使用默认的解密资源回调函数
        /// </summary>
        /// <param name="decryptResourceCallback"></param>
        void SetDecryptResourceCallback(DecryptResourceCallback decryptResourceCallback);

        /// <summary>
        /// 设置资源辅助器
        /// </summary>
        /// <param name="resourceHelper"></param>
        void SetResourceHelper(IResourceHelper resourceHelper);

        /// <summary>
        /// 增加加载资源代理辅助器
        /// </summary>
        /// <param name="loadResourceAgentHelper"></param>
        void AddLoadResourceAgentHelper(ILoadResourceAgentHelper loadResourceAgentHelper);

        /// <summary>
        /// 使用单机模式并初始化资源
        /// </summary>
        /// <param name="initResourcesCompleteCallback">使用单机模式并初始化资源完成时的回调函数</param>
        void InitResources(InitResourcesCompleteCallback initResourcesCompleteCallback);

        /// <summary>
        /// 使用可更新模式并检查版本资源列表
        /// </summary>
        /// <param name="latestInternalResourceVersion">最新的内部资源版本号</param>
        /// <returns></returns>
        CheckVersionListResult CheckVersionList(int latestInternalResourceVersion);

        /// <summary>
        /// 使用可更新模式并更新版本资源列表
        /// </summary>
        /// <param name="versionListLength"></param>
        /// <param name="versionListHashCode"></param>
        /// <param name="versionListCompressedLength"></param>
        /// <param name="versionListCompressedHashCode"></param>
        /// <param name="updateVersionListCallbacks"></param>
        void UpdateVersionList(int versionListLength, int versionListHashCode, int versionListCompressedLength,
            int versionListCompressedHashCode, UpdateVersionListCallback updateVersionListCallbacks);
        
        /// <summary>
        /// 使用可更新模式并校验资源
        /// </summary>
        /// <param name="verifyResourceLengthPerFrame">每帧至少校验资源的大小，以字节为单位</param>
        /// <param name="verifyResourceCompleteCallback">使用可更新模式并校验资源完成时的回调函数</param>
        void VerifyResources(int verifyResourceLengthPerFrame,
            VerifyResourceCompleteCallback verifyResourceCompleteCallback);

        /// <summary>
        /// 使用可更新模式并检查资源
        /// </summary>
        /// <param name="ignoreOtherVariant">是否忽略处理其他变体的资源，若不忽略，将会移除其他变体的资源</param>
        /// <param name="checkResourcesCompleteCallback"></param>
        void CheckResource(bool ignoreOtherVariant, CheckResourcesCompleteCallback checkResourcesCompleteCallback);

        /// <summary>
        /// 使用可更新模式并应用资源包资源
        /// </summary>
        /// <param name="resourcePackPath">要应用的资源包路径</param>
        /// <param name="applyResourcesCompleteCallback">使用可更新模式并应用资源包资源完成时的回调函数</param>
        void ApplyResources(string resourcePackPath, ApplyResourcesCompleteCallback applyResourcesCompleteCallback);


        /// <summary>
        /// 使用可更新模式并更新所有资源
        /// </summary>
        /// <param name="updateResourcesCompleteCallback"></param>
        void UpdateResources(UpdateResourcesCompleteCallback updateResourcesCompleteCallback);
        
        /// <summary>
        /// 使用可更新模式并更新指定资源组资源
        /// </summary>
        /// <param name="resourceGroupName"></param>
        /// <param name="updateResourcesCompleteCallback"></param>
        void UpdateResources(string resourceGroupName, UpdateResourcesCompleteCallback updateResourcesCompleteCallback);

        /// <summary>
        /// 停止更新资源
        /// </summary>
        void StopUpdateResources();
        /// <summary>
        /// 校验资源包
        /// </summary>
        /// <param name="resourcePackPath"></param>
        /// <returns></returns>
        bool VerifyResourcePack(string resourcePackPath);
        /// <summary>
        /// 获取所有加载资源任务的信息
        /// </summary>
        /// <returns></returns>
        TaskInfo[] GetAllLoadAssetInfos();
        /// <summary>
        /// 获取所有加载资源任务信息
        /// </summary>
        /// <param name="results"></param>
        void GetAllLoadAssetInfos(List<TaskInfo> results);
        /// <summary>
        /// 检查资源是否存在
        /// </summary>
        /// <param name="assetName"></param>
        /// <returns></returns>
        HasAssetResult HasAsset(string assetName);

        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="loadAssetCallbacks"></param>
        void LoadAsset(string assetName, LoadAssetCallbacks loadAssetCallbacks);
        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="assetType"></param>
        /// <param name="loadAssetCallbacks"></param>
        void LoadAsset(string assetName, Type assetType, LoadAssetCallbacks loadAssetCallbacks);
        
                /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="priority">加载资源的优先级。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        void LoadAsset(string assetName, int priority, LoadAssetCallbacks loadAssetCallbacks);

        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadAsset(string assetName, LoadAssetCallbacks loadAssetCallbacks, object userData);

        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="assetType">要加载资源的类型。</param>
        /// <param name="priority">加载资源的优先级。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        void LoadAsset(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks);

        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="assetType">要加载资源的类型。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadAsset(string assetName, Type assetType, LoadAssetCallbacks loadAssetCallbacks, object userData);

        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="priority">加载资源的优先级。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadAsset(string assetName, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData);

        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="assetType">要加载资源的类型。</param>
        /// <param name="priority">加载资源的优先级。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadAsset(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData);

        /// <summary>
        /// 卸载资源
        /// </summary>
        /// <param name="asset">要卸载的资源</param>
        void UnloadAsset(object asset);
        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="sceneAssetName"></param>
        /// <param name="loadSceneCallbacks"></param>
        void LoadScene(string sceneAssetName, LoadSceneCallbacks loadSceneCallbacks);
        
        /// <summary>
        /// 异步加载场景。
        /// </summary>
        /// <param name="sceneAssetName">要加载场景资源的名称。</param>
        /// <param name="priority">加载场景资源的优先级。</param>
        /// <param name="loadSceneCallbacks">加载场景回调函数集。</param>
        void LoadScene(string sceneAssetName, int priority, LoadSceneCallbacks loadSceneCallbacks);

        /// <summary>
        /// 异步加载场景。
        /// </summary>
        /// <param name="sceneAssetName">要加载场景资源的名称。</param>
        /// <param name="loadSceneCallbacks">加载场景回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadScene(string sceneAssetName, LoadSceneCallbacks loadSceneCallbacks, object userData);

        /// <summary>
        /// 异步加载场景。
        /// </summary>
        /// <param name="sceneAssetName">要加载场景资源的名称。</param>
        /// <param name="priority">加载场景资源的优先级。</param>
        /// <param name="loadSceneCallbacks">加载场景回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        void LoadScene(string sceneAssetName, int priority, LoadSceneCallbacks loadSceneCallbacks, object userData);

        /// <summary>
        /// 异步卸载场景
        /// </summary>
        /// <param name="sceneAssetName"></param>
        /// <param name="unloadSceneCallbacks"></param>
        void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks);

        /// <summary>
        /// 异步卸载场景
        /// </summary>
        /// <param name="sceneAssetName"></param>
        /// <param name="unloadSceneCallbacks"></param>
        /// <param name="userData"></param>
        void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks, object userData);

        /// <summary>
        /// 获取二进制资源的实际路径
        /// </summary>
        /// <param name="binaryAssetName">要获取实际路径的二进制资源的名称</param>
        /// <returns></returns>、
        /// <remarks>此方法仅适用于二进制资源存储在此磁盘(而非文件系统)中的情况，若二进制资源存储在文件系统中时，返回值将始终为空</remarks>
        string GetBinaryPath(string binaryAssetName);

        /// <summary>
        /// 获取二进制资源的实际路径
        /// </summary>
        /// <param name="binaryAssetName">要获取实际路径的二进制资源名称</param>
        /// <param name="storageInReadOnly">二进制资源是否存储在只读区中</param>
        /// <param name="storageInFileSystem">二进制路径是否存储在文件系统中</param>
        /// <param name="relativePath">二进制资源或存储二进制资源的文件系统，相对于只读区或者读写区的相对路径</param>
        /// <param name="fileName">若二进制资源存储在文件系统中，则指示二进制资源在文件系统中的名称，否则此参数返回空</param>
        /// <returns>是否获取二进制资源的实际路径成功</returns>
        bool GetBinaryPath(string binaryAssetName, out bool storageInReadOnly, out bool storageInFileSystem, out string relativePath, 
            out string fileName);

        /// <summary>
        /// 获取二进制资源的长度
        /// </summary>
        /// <param name="binaryAssetName">要获取长度的二进制资源的名称</param>
        /// <returns></returns>
        int GetBinaryLength(string binaryAssetName);
        /// <summary>
        /// 异步加载二进制资源
        /// </summary>
        /// <param name="binaryAssetName"></param>
        /// <param name="loadBinaryCallbacks"></param>
        void LoadBinary(string binaryAssetName, LoadBinaryCallbacks loadBinaryCallbacks);

        /// <summary>
        /// 异步加载二进制资源
        /// </summary>
        /// <param name="binaryAssetName"></param>
        /// <param name="loadBinaryCallbacks"></param>
        /// <param name="userData"></param>
        void LoadBinary(string binaryAssetName, LoadBinaryCallbacks loadBinaryCallbacks, object userData);

        /// <summary>
        /// 从文件系统中加载二进制资源
        /// </summary>
        /// <param name="binaryAssetName">要加载二进制资源的名称</param>
        /// <returns>存储加载二进制资源的二进制流</returns>
        byte[] LoadBinaryFromFileSystem(string binaryAssetName);

        /// <summary>
        /// 从文件系统中加载二进制资源
        /// </summary>
        /// <param name="binaryAssetName"></param>
        /// <param name="buffer">存储加载二进制资源的二进制流</param>
        /// <returns></returns>
        int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer);
        
        /// <summary>
        /// 从文件系统中加载二进制资源。
        /// </summary>
        /// <param name="binaryAssetName">要加载二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源的二进制流。</param>
        /// <param name="startIndex">存储加载二进制资源的二进制流的起始位置。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex);

        /// <summary>
        /// 从文件系统中加载二进制资源。
        /// </summary>
        /// <param name="binaryAssetName">要加载二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源的二进制流。</param>
        /// <param name="startIndex">存储加载二进制资源的二进制流的起始位置。</param>
        /// <param name="length">存储加载二进制资源的二进制流的长度。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>存储加载二进制资源片段内容的二进制流。</returns>
        byte[] LoadBinarySegmentFromFileSystem(string binaryAssetName, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="offset">要加载片段的偏移。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>存储加载二进制资源片段内容的二进制流。</returns>
        byte[] LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer);
        
                /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="offset">要加载片段的偏移。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>存储加载二进制资源片段内容的二进制流。</returns>
        byte[] LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <param name="startIndex">存储加载二进制资源片段内容的二进制流的起始位置。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="offset">要加载片段的偏移。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="offset">要加载片段的偏移。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer, int length);

        /// <summary>
        /// 从文件系统中加载二进制资源的片段。
        /// </summary>
        /// <param name="binaryAssetName">要加载片段的二进制资源的名称。</param>
        /// <param name="offset">要加载片段的偏移。</param>
        /// <param name="buffer">存储加载二进制资源片段内容的二进制流。</param>
        /// <param name="startIndex">存储加载二进制资源片段内容的二进制流的起始位置。</param>
        /// <param name="length">要加载片段的长度。</param>
        /// <returns>实际加载了多少字节。</returns>
        int LoadBinarySegmentFromFileSystem(string binaryAssetName, int offset, byte[] buffer, int startIndex, int length);

        /// <summary>
        /// 检查资源组是否存在
        /// </summary>
        /// <param name="resourceGroupName">要检查资源组的名称</param>
        /// <returns>资源组是否存在</returns>
        bool HasResourceGroup(string resourceGroupName);

        /// <summary>
        /// 获取默认资源组
        /// </summary>
        /// <returns></returns>
        IResourceGroup GetResourceGroup();

        /// <summary>
        /// 获取资源组
        /// </summary>
        /// <param name="resourceGroupName"></param>
        IResourceGroup GetResourceGroup(string resourceGroupName);

        /// <summary>
        /// 获取所有资源组
        /// </summary>
        /// <returns></returns>
        IResourceGroup[] GetAllResourceGroups();

        /// <summary>
        /// 获取所有资源组
        /// </summary>
        /// <param name="results"></param>
        void GetAllResourceGroups(List<IResourceGroup> results);

        /// <summary>
        /// 获取资源组集合
        /// </summary>
        /// <param name="resourceGroupNames"></param>
        /// <returns></returns>
        IResourceGroupCollection GetResourceGroupCollection(params string[] resourceGroupNames);

        /// <summary>
        /// 获取资源组集合
        /// </summary>
        /// <param name="resourceGroupNames"></param>
        /// <returns></returns>
        IResourceGroupCollection GetResourceGroupCollection(List<string> resourceGroupNames);
    }
}